{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "594674\n",
      "aaa\n",
      "郭靖,2612\n",
      "黄蓉,1749\n",
      "甚么,1254\n",
      "洪七公,1057\n",
      "说道,1043\n",
      "欧阳锋,1043\n",
      "一个,998\n",
      "自己,971\n",
      "师父,875\n",
      "黄药师,868\n"
     ]
    }
   ],
   "source": [
    "import jieba\n",
    "\n",
    "# 分析射雕英雄传 词频\n",
    "# 1. 读取文件\n",
    "f = open(\"d:\\\\射雕英雄传-网络版.txt\",\"r\",encoding=\"utf-8\")\n",
    "s = f.read()\n",
    "f.close()\n",
    "# print(s)\n",
    "# 2. 分词\n",
    "ls = jieba.lcut(s)\n",
    "print(len(ls))\n",
    "print(\"aaa\")\n",
    "# 3. 统计\n",
    "d = {} #{\"郭靖\":100,\"黄蓉\":90}\n",
    "for word in ls:\n",
    "    if len(word)>=2:\n",
    "        d[word] = d.get(word,0) + 1\n",
    "# 4. 排序输出\n",
    "lt = list(d.items()) # [(\"郭靖\",100),(\"黄蓉\",90),(\"洪七公\",10))\n",
    "lt.sort(key=lambda x:x[1],reverse=True)\n",
    "# 5. 打印输出\n",
    "for i in range(10):\n",
    "    print(\"{},{}\".format(lt[i][0],lt[i][1]))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
